package com.dhnsoft.boot3.rpc.config;

import com.dhnsoft.boot3.rpc.service.ExpressInterface;
import com.dhnsoft.boot3.rpc.service.WeatherInterface;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

/**
 * @author dhn
 * @version 1.0
 * @date 2023-09-12 16:25
 * @Desc
 */
@Configuration
public class WeatherConfiguration {

    @Bean
    HttpServiceProxyFactory factory(@Value("${aliyun.appcode}") String appcode){
        //1.创建客户端
        WebClient client = WebClient.builder()
                .defaultHeader("Authorization","APPCODE "+appcode)
                .codecs(clientCodecConfigurer -> {
                    clientCodecConfigurer
                            .defaultCodecs()
                            .maxInMemorySize(256*1024*1024);
                    //响应数据量太大有可能会超出BufferSize，所以这里设置大一点
                })
                .build();
        //2.创建工厂
        HttpServiceProxyFactory factory = HttpServiceProxyFactory
                .builder(WebClientAdapter.forClient(client)).build();
        return factory;
    }



    //天气预报API接口
    @Bean
    WeatherInterface weatherInterface(HttpServiceProxyFactory factory){


        //3.获取代理对象
        WeatherInterface weatherAPI = factory.createClient(WeatherInterface.class);
        return weatherAPI;
    }

    //邮件查询API接口
    @Bean
    ExpressInterface expressInterface(HttpServiceProxyFactory factory){
        ExpressInterface client = factory.createClient(ExpressInterface.class);
        return client;
    }


}
